package com.highspeed.bdk.gateway.socket.analyze.server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 模拟网关向客户端发送消息，该消息实际为从网关读取回来的消息
 */
public class Server {
    private ServerSocket serverSocket;

    public Server() {
    }

    public Server(int port) {
        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void receive() {
        try {
            Socket socket = serverSocket.accept();
            InputStream is = socket.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            OutputStream os = socket.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(os);
            int len = -1;
            byte[] bytes = new byte[1024];
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
            bos.close();
            bis.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        int port = 9999;
        new Server(port).receive();
    }

}
